﻿using System;
using System.Collections.Generic;
using System.Linq;
using Edu.Web.Data.DAO;
using Edu.Web.Data.DV;
using Edu.Web.Data.Entity;
using ESoft.Core.Data.Service.Impl;
using ESoft.Core.Interceptor;

namespace Edu.Web.Data.Service.Impl
{
    public class CourceExamService : BaseService<CourceExamEntity, long>, ICourceExamService
    {
        private readonly ICourceExamDAO _courceScoreDao;
        private readonly ICourceExamScoreDAO _courceExamScoreDAO;

        public CourceExamService(ICourceExamDAO courceScoreDao, ICourceExamScoreDAO courceExamScoreDAO) : base(courceScoreDao)
        {
            _courceScoreDao = courceScoreDao;
            _courceExamScoreDAO = courceExamScoreDAO;
        }

        public List<CourceScoreDV> GetCourceScoreByCreateUserId(long createUserId)
        {
            return _courceScoreDao.GetCourceScoreList().Where(x => x.CreateUserId == createUserId).ToList();
        }

        public List<CourceScoreDV> GetCourceScoreByStuId(long userId)
        {
            return _courceScoreDao.GetCourceScoreList().Where(x => x.UserId == userId).OrderByDescending(x => x.CreateTime).ToList();
        }

        [Transactional]
        public void SaveExam(CourceExamEntity courceExamVO, List<CourceExamScoreEntity> courceExamScoreList)
        {
            var result = _courceScoreDao.Add(courceExamVO);
            foreach (var courceExamScore in courceExamScoreList)
            {
                courceExamScore.CourceExamId = result.Id;
                _courceExamScoreDAO.Add(courceExamScore);
            }
        }
    }
}